﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Microsoft.Phone.Controls;

namespace WinPhoneRatingReminder
{
    public partial class ConfirmationBox : UserControl
    {
        public Action YesAction;
        public Action NoAction;
        public Action RemindAction;
        private static bool AppBarVisibility { get; set; }
        
        private PhoneApplicationFrame RootFrame
        {
            get
            {
                return Application.Current.RootVisual as PhoneApplicationFrame;
            }
        }

        private PhoneApplicationPage CurrentPage
        {
            get
            {
                PhoneApplicationPage currentPage = null;
                if (RootFrame != null)
                {
                    currentPage = RootFrame.Content as PhoneApplicationPage;
                }

                return currentPage;
            }
        }

        public ConfirmationBox()
        {
            InitializeComponent();
        }


        public static void Show(string title, string message, Action yesAction, Action noAction, Action remindAction)
        {
            var confBox = new ConfirmationBox
            {
                TitleTextBlock = { Text = title },
                MessageTextBlock = { Text = message },
                YesButton = { Content = "Yes" },
                NoButton = { Content = "No" },
                RemindButton = { Content = "Remind me later" },
                YesAction = yesAction,
                NoAction = noAction,
                RemindAction = remindAction
            };
            confBox.Show();
        }

        private void Show()
        {
            if (CurrentPage.ApplicationBar != null)
                CurrentPage.ApplicationBar.IsVisible = false;

            var popUp = new Popup {Child = this, IsOpen = true};

        }

        private void Hide()
        {
            if (CurrentPage.ApplicationBar != null)
                CurrentPage.ApplicationBar.IsVisible = true;

            var popUp = this.Parent as Popup;
            if (popUp != null) popUp.IsOpen = false;
        }
        
        private void YesButtonClick(object sender, RoutedEventArgs e)
        {
            YesAction.Invoke();
            Hide();
        }

        private void NoButtonClick(object sender, RoutedEventArgs e)
        {
            NoAction.Invoke();
            Hide();
        }

        private void RemindButtonClick(object sender, RoutedEventArgs e)
        {
            RemindAction.Invoke();
            Hide();
        }
    }
}